package com.yix.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static void main(String[] args) throws ParseException {
        long startTime1 = 1530613938532l;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date testDate = simpleDateFormat.parse("2021-1-18");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(testDate);
        System.out.println("周天"+ calendar.get(Calendar.WEEK_OF_YEAR));
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);//设置星期一为一周开始的第一天
        System.out.println("周天"+ calendar.get(Calendar.WEEK_OF_YEAR));
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        System.out.println("周一"+ calendar.get(Calendar.WEEK_OF_YEAR));
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        calendar.setTimeInMillis(System.currentTimeMillis());//获得当前的时间戳
        int weekYear = calendar.get(Calendar.YEAR);//获得当前的年
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周

        System.out.println("第几周："+weekOfYear);
        calendar.setWeekDate(weekYear, weekOfYear, 2);//获得指定年的第几周的开始日期
        long starttime = calendar.getTime().getTime();//创建日期的时间该周的第一天，
        calendar.setWeekDate(weekYear, weekOfYear, 1);//获得指定年的第几周的结束日期
        long endtime = calendar.getTime().getTime();

        String dateStart = simpleDateFormat.format(starttime);//将时间戳格式化为指定格式
        String dateEnd = simpleDateFormat.format(endtime);
        System.out.println(dateStart);
        System.out.println(dateEnd);
    }
}
